package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.entity.MemberInfoEntity;
import com.aduerr.v1shouyin.entity.MemberToPayEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.util.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.math.BigDecimal;
import java.util.HashMap;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/5/22 9:28
 * @change
 * @chang time
 * @class describe
 */

public class NewMemberToPayActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_member_card_no)
    TextView tvMemberCardNo;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.tv_level_discount)
    TextView tvLevelDiscount;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_discount_money)
    TextView tvDiscountMoney;
    @BindView(R.id.tv_real_money)
    TextView tvRealMoney;
    @BindView(R.id.bt_right)
    Button btRight;
    @BindView(R.id.bt_left)
    Button btLeft;
    @BindView(R.id.tv_red_money)
    TextView tvRedMoney;
    @BindView(R.id.tv_coupon_money)
    TextView tvCouponMoney;

    private MemberInfoEntity memberInfo;
    private String money, scanMemberNo, beizhu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_member_to_pay);
        ButterKnife.bind(this);
        tvTitle.setText("会员收款");
        memberInfo = (MemberInfoEntity) getIntent().getSerializableExtra(
                "memberinfo");
        money = getIntent().getStringExtra("realMoney");
        scanMemberNo = getIntent().getStringExtra("scanMemberNo");
        beizhu = getIntent().getStringExtra("beizhu");
        fillData(memberInfo.getData());
        getMemberConsumeInfo(memberInfo.getData().getNO(), money);
    }

    private void fillData(MemberInfoEntity.DataBean member) {
        tvMemberCardNo.setText(member.getNO());
        tvMemberName.setText(member.getNames());
        tvLevelDiscount.setText(member.getDiscount() + "折");
        tvPhone.setText(member.getPhone());
        tvBalance.setText(member.getBalance());
        tvMoney.setText(money);
    }

    @OnClick({R.id.img_back, R.id.bt_left, R.id.bt_right})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.bt_left:
                Intent intent = new Intent(NewMemberToPayActivity.this,
                        ScanningActivity.class);
                intent.putExtra(Constants.QRCODE_CAPUTER, Constants.MEMBER_CONSUME_NEW);
                intent.putExtra(Constants.REALMONEY,
                        tvRealMoney.getText().toString().replace("元", ""));
                intent.putExtra(Constants.ORDER_MONEY, money);
                intent.putExtra(Constants.MEMBERID, memberInfo.getData().getNO());
                intent.putExtra("beizhu", beizhu);
                intent.putExtra("isMember",true);
                startActivity(intent);
                finish();
                break;
            case R.id.bt_right:
                if(new BigDecimal(tvRealMoney.getText().toString().replace("元", "")).compareTo(new BigDecimal(tvBalance.getText().toString().replace("元", ""))) == 1) {
                    ToastUtils.showToast(this, "余额不足");
                    return;
                }
                btRight.setEnabled(false);
                HashMap<String, String> addParams = new HashMap<>();
                addParams.put("MemberId", memberInfo.getData().getNO());
                addParams.put("OrderMoney", money);
                addParams.put("Remark", beizhu);
                APIRetrofit.getAPIService().memberConsume(RXRequest.getXingPosParams(addParams
                        , this))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(entity -> {

                            if(entity.getSuccess() == 1) {
                                MemberToPayEntity memberToPayEntity =
                                        new MemberToPayEntity();
                                MemberToPayEntity.DataBean dataBean =
                                        new MemberToPayEntity.DataBean();
                                dataBean.setBalance(entity.getData().getBalance() + "");
                                dataBean.setChangeint(Integer.parseInt("0"));
                                dataBean.setChangemoney(entity.getData().getPayMoney() + "");
                                dataBean.setDiscount(entity.getData().getLevelDiscount() + "");
                                dataBean.setName(memberInfo.getData().getNames());
                                dataBean.setOrderid(entity.getData().getOrderId());
                                dataBean.setPhone(memberInfo.getData().getPhone());
                                dataBean.setSendmoney(entity.getData().getSellerHongBaoAmount() + "");
                                dataBean.setIntegral(Integer.parseInt(memberInfo.getData().getIntegral()));
                                memberToPayEntity.setData(dataBean);

                                Intent member = new Intent(NewMemberToPayActivity.this,
                                        ResultActivity.class);
                                member.putExtra("money",
                                        memberToPayEntity.getData().getChangemoney().replace("-", ""));
                                member.putExtra("beizhu", beizhu);
                                member.putExtra("sucess", "6");
                                member.putExtra("scanMemberNo", scanMemberNo);
                                member.putExtra("memberInfo",
                                        memberToPayEntity.getData());
                                member.putExtra("datebeen", memberToPayEntity);
//                                btRight.setEnabled(true);
                                startActivity(member);
                                finish();
                            } else {
                                ToastUtils.showToast(this,
                                        entity.getErrMsg());
                            }
                        }, throwable -> throwable.printStackTrace());

                break;
        }
    }

    private void getMemberConsumeInfo(String memberId, String orderMoney) {
        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("MemberId", memberId);
        addParams.put("OrderMoney", orderMoney);
        APIRetrofit.getAPIService().getMemberConsumeInfo(RXRequest.getXingPosParams(addParams
                , this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(entity -> {
                    if(entity.getSuccess() == 1) {
                        tvDiscountMoney.setText(entity.getData().getLevelDiscount() +
                                "元");
                        tvRealMoney.setText(entity.getData().getPayMoney() + "元");
                        tvRedMoney.setText(entity.getData().getSellerHongBaoAmount() +
                                "元");
                        tvCouponMoney.setText(entity.getData().getSellerCouponAmount() + "元");

                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    ToastUtils.showToast(this, "网络异常");
                });
    }
}
